import React from 'react';
import {Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message} from 'antd';
const R = require('ramda');
import OperatorConstant from "../../../utils/OperatorConstant";
import BizParams from '../../../components/commons/BizParams'
import CurrencySelect from '../../../components/commons/CurrencySelect'
import SingleDragger from "../../commons/SingleDragger";

const Dragger = Upload.Dragger;
const TabPane = Tabs.TabPane;
const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const Option = Select.Option;

export default Form.create({wrappedComponentRef: true})(class AgreementInfo extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            tradeExportAgreement: "",
            businessLicenses: "",
            corporateIdCard: "",
            type: R.isNil(this.props.type) ? "" : this.props.type,
        };
    }


    resetUpload=(refs)=>{
     for(let i in refs){
         refs[i].resetUpload();
     }
    };

    handleResetFields = () => {
        this.resetUpload(this.refs);
        this.props.form.resetFields();
    }

    handleCustomer = () => {
        if(this.state.type ==OperatorConstant.add){
            return false;
        }else if(this.props.cstInfo.csttype === '1'){
            return true;
        }
    }

    handleSaveAttachment = () => {
        if(this.refs.tradeExportAgreement.getUrl()=="") {
            message.error("请上传公司协议 | 外贸进口协议!");
            return;

        }
        if(this.refs.businessLicenses.getUrl()=="") {
            message.error("请上传公司资质 | 统一社会信用代码!");
            return;

        }
        if(this.refs.corporateIdCard.getUrl()=="") {
            message.error("请上传法人代表身份证!");
            return;

        }

        this.props.form.validateFields((errors, values) => {
            if (errors) return;

            let type =  'customerManage/entqualification';
            let form ={};

            let customer = R.isNil(this.props.cstInfo) ? {} : this.props.cstInfo;
            form.tradeExportAgreement = R.isNil(this.refs.tradeExportAgreement.getUrl())?'':this.refs.tradeExportAgreement.getUrl();
            form.businessLicenses = R.isNil(this.refs.businessLicenses.getUrl())?'':this.refs.businessLicenses.getUrl();
            form.corporateIdCard = R.isNil(this.refs.corporateIdCard.getUrl())?'':this.refs.corporateIdCard.getUrl();
            form.csttype = customer.csttype;
            form.id = this.props.cstid;
            this.props.dispatch({
                type: type,
                payload: {
                    form: form,
                    onSuccess:()=>{
                        //this.resetUpload(this.refs);
                        // this.props.customerHandleCancel();
                    }
                },
            });


        });



    };




    render() {
        let formItemLayout = {
            labelCol: { xs: { span: 24 }, sm: { span: 10 } },
            wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
        };

        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        if (!R.isNil(dataSource)) {
             this.state.businessLicenses=dataSource.businessLicenses;
             this.state.corporateIdCard=dataSource.corporateIdCard;
             this.state.tradeExportAgreement=dataSource.tradeExportAgreement;
        }


        let module="agreementInfo";

        return (
            <Form>
                <Tabs tabPosition={'top'}>
                    <TabPane tab="附件" key="1">
                        <Row>
                            <Col span={12} style={{display: this.handleCustomer() ?"none" : "inline-block" }}>
                                <FormItem {...formItemLayout} label={<span className="ant-form-item-required">公司协议 | 外贸进口协议</span>} hasFeedback>

                                    <SingleDragger url={R.isNil(this.state.tradeExportAgreement)?'':this.state.tradeExportAgreement.url} dataSource={dataSource} ref="tradeExportAgreement" module={module} disabled={this.props.disabled}/>
                                </FormItem>
                            </Col>
                            <Col span={12} style={{display: this.handleCustomer() ?"none" : "inline-block" }}>
                                <FormItem {...formItemLayout} label={<span className="ant-form-item-required">公司资质 | 统一社会信用代码</span>} hasFeedback>
                                    <SingleDragger url={R.isNil(this.state.businessLicenses)?'':this.state.businessLicenses.url} dataSource={dataSource} ref="businessLicenses" module={module} disabled={this.props.disabled}/>

                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={12} style={{display: this.handleCustomer() ?"none" : "inline-block" }}>
                                <FormItem {...formItemLayout} label={<span className="ant-form-item-required">法人代表身份证</span>} hasFeedback>
                                    <SingleDragger url={R.isNil(this.state.corporateIdCard)?'':this.state.corporateIdCard.url} dataSource={dataSource} ref="corporateIdCard" module={module} disabled={this.props.disabled}/>

                                </FormItem>
                            </Col>
                        </Row>
                    </TabPane>
                </Tabs>
            </Form>
        )
    }
})


// WEBPACK FOOTER //
// ./public/src/components/customer/components/AgreementInfo.js